var models = require('../../models');
var mediaInfoGet = require('../../tool/mediaInfoGet.js');
var count = 20;
module.exports = function (req, res) {
    //noinspection JSCheckFunctionSignatures
    var page = req.param('page') || 1;
    //noinspection JSCheckFunctionSignatures
    var isAjax = req.param('ajax');
    var start = (page - 1) * count;
    var mediaModel = models['media'];
    var callback = function (err, docs) {
        if (err) {
            console.log(err);
            return;
        }
        //noinspection JSUnresolvedFunction
        res.locals = mediaInfoGet.mix(res.locals, mediaInfoGet);
        if (isAjax) {
            res.json(docs);
        } else {
            res.render('media/list', {
                'title':'Show This',
                'data':docs
            }, 0);
        }
    };
    var userFields = ['username', 'description', 'headpic', '_id'];
    var query = mediaModel.find();
    query.populate('author', userFields.join(' '));
    query.skip(start).limit(count);
    query.sort({'_id':-1});
    query.exec(callback);
};